<?php
namespace App\Service\Admin;

class ZixunService extends BaseService
{
    //咨询

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUdsPatient);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $cate_name = '';
            $uname = '';
            $dname = '';
            //咨询类型
            $cateOne = (array) $this->dbConn($this->dbCate)
                ->where('id', $v['seek_id'])
                ->first();
            $cate_name = empty($cateOne) ? '-' : $cateOne['title'];
            //用户
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['patient_id'])
                ->first();
            if (!empty($uOne) && $uOne['uname']) {
                $uname = $uOne['uname'];
            }
            //医生
            $dOne = (array) $this->dbConn($this->dbDoctor)
                ->where('id', $v['doctor_id'])
                ->first();
            $duid = empty($dOne) ? 0 : $dOne['uid'];
            $udOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $duid)
                ->first();
            if (!empty($udOne) && $udOne['uname']) {
                $dname = $udOne['uname'];
            }
            $arys[] = [
                'id' => $v['id'],
                'cate_name' => $cate_name,
                'uname' => $uname,
                'dname' => $dname,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}